package com.deng.viewhigh.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.deng.common.SQLHelper;
import com.deng.viewhigh.vo.OrgCbVo;
import com.deng.viewhigh.vo.OrgKpiVo;

public class OrgCbImpl extends SQLHelper {

	public List<OrgCbVo> getOrganizationVsCb(int organization_id)
	{
		try
		{
			List<OrgCbVo> lst = new ArrayList<OrgCbVo>();
			Connection conn = getConnect();
			String sql = "select v.dept_code,v.dept_name,vs.organization_id from HERP3.dbo.VIEW_JJ_CB_DEPT as v" +
					" left join t_sys_stat_department_vs_cb_organization as vs" +
					" on v.dept_code = vs.cb_org_id" +
					" and vs.organization_id = "+organization_id;
			
			ResultSet rs = queryData(conn, sql);
			while(rs.next())
			{
				OrgCbVo vo = new OrgCbVo();
				vo.organization_id = rs.getInt("organization_id");
				vo.new_organization_id = rs.getInt("organization_id");
				vo.orgCbId = rs.getString("dept_code");
				vo.cbName = rs.getString("dept_name");
				lst.add(vo);
			}
			freeConnection(conn);
			
//			OrgCbVo vo = new OrgCbVo();
//			vo.organization_id = 7;
//			vo.new_organization_id = 7;
//			vo.orgCbId = "10100010";
//			vo.cbName = "儿科一";
//			lst.add(vo);
//			
//			vo = new OrgCbVo();
//			vo.organization_id = 0;
//			vo.new_organization_id = 0;
//			vo.orgCbId = "10120010";
//			vo.cbName = "儿科儿";
//			lst.add(vo);
//			
//			vo = new OrgCbVo();
//			vo.organization_id = 0;
//			vo.new_organization_id = 0;
//			vo.orgCbId = "10100010";
//			vo.cbName = "儿科三";
//			lst.add(vo);
			
			return lst;
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}
		return null;
	}
	
	public String updateOrganizationVsCb(List<OrgCbVo> l)
	{
		try
		{
			Connection conn = getConnect();
			String sql = "";
			for(OrgCbVo vo:l)
			{
				if(vo.organization_id != vo.new_organization_id)//不同
				{
					if(vo.new_organization_id == 0)//删除
					{
						sql = "delete t_sys_stat_department_vs_cb_organization where cb_org_id = '"+vo.orgCbId+"'" +
								" and organization_id = "+vo.organization_id;
					}
					else//增加
					{
						sql = "insert into t_sys_stat_department_vs_cb_organization (cb_org_id,organization_id)" +
								" values('"+vo.orgCbId+"',"+vo.new_organization_id+")";
					}
					updateData(conn, sql);
				}
			}
			freeConnection(conn);
			return null;
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}
		return "导入数据报错，请联系管理员";
	}
	
}
